package com.xiyou.luckdraw.common.utils;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanMap;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

public class ExportUtils {
    public static void createManySheetExcel(HttpServletRequest request, HttpServletResponse response, HSSFWorkbook hss, String title, Integer[] columnWidths, String[] cellValues, List<Map<String, Object>> list, String[] titlename) {
        //HSSFWorkbook hss = new HSSFWorkbook();
        HSSFSheet sheet = hss.createSheet(title);
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < columnWidths.length; i++) {
            int width = columnWidths[i];
            sheet.setColumnWidth(i, width * 256);
        }

        HSSFCellStyle style = hss.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        sheet.setDefaultColumnStyle(0, style);
        HSSFCell cell = row.createCell(0);
        for (int i = 0; i < cellValues.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(cellValues[i]);
            cell.setCellStyle(style);

        }
        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);
            Map<String, Object> map = list.get(i);
            for (int j = 0; j < titlename.length; j++) {
                HSSFCell cells = row.createCell(j);
                Object value = map.get(titlename[j]);
                String valuestr = (value == null) ? "" : value.toString();
                cells.setCellValue(valuestr);
                cells.setCellStyle(style);
            }
        }

    }

    public static void createExcel(HttpServletRequest request, HttpServletResponse response, String title, Integer[] columnWidths, String[] cellValues, List<Map<String, Object>> list, String[] titlename) {
        HSSFWorkbook hss = new HSSFWorkbook();
        HSSFSheet sheet = hss.createSheet(title);
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < columnWidths.length; i++) {
            int width = columnWidths[i];
            sheet.setColumnWidth(i, width * 256);
        }

        HSSFCellStyle style = hss.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        sheet.setDefaultColumnStyle(0, style);
        HSSFCell cell = row.createCell(0);
        for (int i = 0; i < cellValues.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(cellValues[i]);
            cell.setCellStyle(style);

        }
        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);


           /* Object value = list.get(i);
            Map map = new BeanMap(list.get(i));
            for (int j = 0; j < titlename.length; j++) {
                HSSFCell cells = row.createCell(j);
                Object value = map.get(titlename[j]);
                String valuestr = (map.get(titlename[j]) == null) ? "" : map.get(titlename[j]).toString();
//                String valuestr = (value == null) ? "" : value.toString();
                cells.setCellValue(valuestr);
                cells.setCellStyle(style);
            }*/


            Map<String, Object> map = list.get(i);
            for (int j = 0; j < titlename.length; j++) {
                HSSFCell cells = row.createCell(j);
                Object value = map.get(titlename[j]);
                String valuestr = (value == null) ? "" : value.toString();
                cells.setCellValue(valuestr);
                cells.setCellStyle(style);
            }
        }
        OutputStream ouputStream = null;
        try {
            String agent = request.getHeader("User-Agent");
            boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
            String FileName = "";
            if (isMSIE) {
                FileName = java.net.URLEncoder.encode(title, "UTF8");
            } else {
                //FileName = URLEncoder.encode(title, "UTF-8");
                FileName = new String(title.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + FileName + ".xls");
            ouputStream = response.getOutputStream();
            hss.write(ouputStream);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ouputStream.flush();
                ouputStream.close();
            } catch (Exception e) {

            }

        }
    }
}
